﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class InformationBiz
    {
        /// <summary>
        /// 获取资迅列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="infoType"></param>
        /// <param name="status"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Information> GetInfoListByPage(int page, int pageSize, int infoType, int status, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var expression = from i in ctx.Information where (i.InfoType == infoType || i.InfoType == -1) && (i.Status == status || i.Status == -2)
                                     orderby i.CreateTime descending
                                     select i;
                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(i => new Information().Set(i)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 添加资迅信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="infoFrom"></param>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static bool InformationAdd(Information infor)
        {
            try
            {
                var info = new Information()
                {
                    CreateUser = System.Web.HttpContext.Current.User.Identity.Name,
                    CreateTime = DateTime.Now,
                    Status = ItemStatus.Enable,
                    Content = infor.Content,
                    InfoFrom = infor.InfoFrom,
                    InfoType = infor.InfoType,
                    Title = infor.Title
                };
                return info.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
        }

        /// <summary>
        /// 修改资迅信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="infoFrom"></param>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static bool InformationEdit(int id, Information infor)
        {
            try
            {
                var info = new Information().Find(id.ToString());
                if (info != null)
                {
                    info.CreateUser = System.Web.HttpContext.Current.User.Identity.Name;
                    info.CreateTime = DateTime.Now;
                    info.Status = ItemStatus.Enable;
                    info.Content = infor.Content;
                    info.InfoFrom = infor.InfoFrom;
                    info.InfoType = infor.InfoType;
                    info.Title = infor.Title;

                    return info.PostModify() != null;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }


        /// <summary>
        /// 修改资迅状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="infoFrom"></param>
        /// <param name="infoType"></param>
        /// <returns></returns>
        public static bool InfoStatusChange(int id, int destinationStatus)
        {
            try
            {
                var info = new Information().Find(id.ToString());
                if (info != null)
                {
                    info.Status = (ItemStatus)destinationStatus;

                    return info.PostModify() != null;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }

    }
}
